package com.example.base.security.service;


import com.alibaba.fastjson.JSONObject;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.domain.SelfUserEntity;
import com.example.base.security.domain.SysUserEntity;
import com.example.base.security.domain.SysUserRoleEntity;
import com.example.base.security.domain.vo.SysUserRegisterVo;
import com.example.base.security.domain.vo.SysUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 15:16
 * @since JDK 1.8
 */
@Component
public class SelfUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询用户信息
     * @Author Sans
     * @CreateTime 2019/9/13 17:23
     * @Param  username  用户名
     * @Return UserDetails SpringSecurity用户信息
     */
    @Override
    public SelfUserEntity loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        SysUserEntity sysUserEntity =sysUserService.selectUserByName(username);
        if (sysUserEntity!=null){
            // 组装参数
            SelfUserEntity selfUserEntity = new SelfUserEntity();
            BeanUtils.copyProperties(sysUserEntity,selfUserEntity);
            return selfUserEntity;
        }
        return null;
    }

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    public SysUserEntity register(SysUserRegisterVo sysUserVo) throws GlobalExcetion {
        if(!sysUserVo.getAgainPassWord().equals(sysUserVo.getPassword())){
            throw new GlobalExcetion(201,"两次密码不一致");
        }
        sysUserService.commonValited(sysUserVo);
        String password=bCryptPasswordEncoder.encode(sysUserVo.getPassword());
        SysUserEntity userEntity=new SysUserEntity();
        BeanUtils.copyProperties(sysUserVo,userEntity);
        userEntity.setPassword(password);
        sysUserService.save(userEntity);
        return userEntity;
    }

    @Autowired
    private SysUserRoleService sysUserRoleService;


    public SysUserEntity addOrUpdateSysUserEntity(SysUserVo vo) throws GlobalExcetion {
        sysUserService.commonValited(vo);
        SysUserEntity sysUserEntity=new SysUserEntity();
        BeanUtils.copyProperties(vo,sysUserEntity);
        String password=bCryptPasswordEncoder.encode(vo.getPassword());
        sysUserEntity.setPassword(password);
        if(vo.getUserId()!=null){
            sysUserService.updateById(sysUserEntity);
        }else {
            sysUserService.save(sysUserEntity);
        }
        if(vo.getRoleList()!=null && vo.getRoleList().size()>0){

            for(Object obj:vo.getRoleList()){
                JSONObject json=(JSONObject) obj;
                Object id=json.get("id");
                Object roleId=json.get("roleId");
                Object deptId=json.get("deptId");
                if(roleId==null || deptId==null){
                    continue;
                }
                if(id==null){
                    SysUserRoleEntity userRoleEntity=new SysUserRoleEntity();
                    userRoleEntity.setRoleId(Integer.parseInt(String.valueOf(roleId)));
                    userRoleEntity.setUserId(sysUserEntity.getUserId());
                    userRoleEntity.setDeptId(Integer.valueOf(String.valueOf(deptId)));
                    sysUserRoleService.save(userRoleEntity);
                }else {
                    SysUserRoleEntity userRoleEntity=sysUserRoleService.getById(String.valueOf(id));
                    if(userRoleEntity==null){
                        continue;
                    }
                    userRoleEntity.setDeptId(Integer.parseInt(String.valueOf(deptId)));
                    userRoleEntity.setRoleId((Integer.parseInt(String.valueOf(roleId))));
                    sysUserRoleService.updateById(userRoleEntity);
                }
            }
        }
        return sysUserEntity;
    }
}
